import http from './http'

export interface Shipment {
  ID?: number
  order_id: number
  shipment_no?: string
  shipment_date: string
  status: string
  delivery_address?: string
  contact_person?: string
  contact_phone?: string
  notes?: string
}

export interface ShipmentSearchParams {
  order_id?: number
  status?: string
  start_date?: string
  end_date?: string
  page?: number
  pageSize?: number
}

export interface ShipmentForm {
  order_id: number
  shipment_date: string
  status: string
  delivery_address?: string
  contact_person?: string
  contact_phone?: string
  notes?: string
}

export interface BatchShipmentForm {
  order_ids: number[]
  shipment_date: string
  status: string
  delivery_address?: string
  contact_person?: string
  contact_phone?: string
  notes?: string
}

export const shipmentsApi = {
  // 获取发货单列表
  getList: (params?: ShipmentSearchParams) => http.get('/shipments', { params }),

  // 获取单个发货单
  getDetail: (id: number) => http.get(`/shipments/${id}`),

  // 创建发货单
  create: (shipment: ShipmentForm) => http.post('/shipments', shipment),

  // 更新发货单状态
  updateStatus: (id: number, status: string) => http.put(`/shipments/${id}/status`, { status }),

  // 删除发货单
  delete: (id: number) => http.delete(`/shipments/${id}`),

  // 获取送货单
  getDeliveryNote: (orderId: number) => http.get(`/delivery_note/${orderId}`),

  // 批量创建发货单
  batchCreate: (data: BatchShipmentForm) => http.post('/shipments/batch', data),

  // 获取发货单统计
  getStats: () => http.get('/shipments/stats')
} 